package code;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.ListIterator;
import java.util.Vector;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


public class AsymmetricEncDec {
	public void encrypt(Key key, String padding, String file){
		try {
			Cipher           cipher = Cipher.getInstance(padding, "BC");
			SecureRandom     random = Utils.createFixedRandom();
			cipher.init(Cipher.ENCRYPT_MODE, key);

			FileInputStream inFile = new FileInputStream(file);
			int bytesRead;
			Vector<byte[]> in = new Vector<byte[]>();
            do
            {
            	byte[] input = new byte[100];
            	bytesRead = inFile.read(input, 0, input.length);
            	if (bytesRead != -1) in.add(input);
            } while  ( bytesRead != -1);
            inFile.close();
            
            Vector<byte[]> out = new Vector<byte[]>();
            ListIterator iterator= in.listIterator();
            while(iterator.hasNext()){
            	byte[] output = cipher.doFinal((byte [])iterator.next());
            	if (output != null) out.add(output);
            }
            
            FileOutputStream outFile = new FileOutputStream(file);
            ListIterator iterator2= out.listIterator();
            while(iterator2.hasNext()){
            	byte[] output2 =(byte [])iterator2.next();
            	outFile.write(output2);
            }
            outFile.close();
	      
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void decrypt(Key key, String padding, String file){
		try {
			Cipher           cipher = Cipher.getInstance(padding, "BC");
			SecureRandom     random = Utils.createFixedRandom();
			cipher.init(Cipher.DECRYPT_MODE, key);

			FileInputStream inFile = new FileInputStream(file);
			int bytesRead;
			Vector<byte[]> in = new Vector<byte[]>();
			do
            {
            	byte[] input = new byte[128];
            	bytesRead = inFile.read(input, 0, input.length);
            	if (bytesRead != -1) in.add(input);
            } while  ( bytesRead != -1);
            inFile.close();
            
            Vector<byte[]> out = new Vector<byte[]>();
            ListIterator iterator= in.listIterator();
            while(iterator.hasNext()){
            	byte[] output = cipher.doFinal((byte [])iterator.next());
            	if (output != null) out.add(output);
            }
            
            FileOutputStream outFile = new FileOutputStream(file);
            ListIterator iterator2= out.listIterator();
            while(iterator2.hasNext()){
            	byte[] output2 =(byte [])iterator2.next();
            	outFile.write(output2);
            }
            outFile.close();
	      
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}